/**
 * reducer动态注入
 */

export class ReducerRegistry {
  constructor() {
    this._emitChange = null;
    this._reducers = {};
  }

  // 返回当前应用的 reducer
  getReducers() {
    return { ...this._reducers };
  }

  /**
   * 注册 reducer
   * @param {String} name reducer的名称
   * @param {Function} reducer reducer 对象
   */
  register(name, reducer) {
    this._reducers = { ...this._reducers, [name]: reducer };

    if (this._emitChange) {
      this._emitChange(this.getReducers());
    }
  }

  setChangeListener(listener) {
    this._emitChange = listener;
  }
}

const reducerRegistry = new ReducerRegistry();
export default reducerRegistry;
